package com.tvunetworks.device.extra.controller.feign;

import com.github.pagehelper.PageInfo;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.gs.model.Device;
import com.tvunetworks.device.extra.common.feign.DeviceExtraInterface;
import com.tvunetworks.device.extra.common.model.*;
import com.tvunetworks.device.extra.jms.MQProducers;
import com.tvunetworks.device.extra.service.AnywhereService;
import com.tvunetworks.device.extra.service.GridService;
import com.tvunetworks.device.extra.service.SourceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;


/**
 * @author Etan Yang
 * @create: 2021/12/3 17:12
 */

@RestController
@RequestMapping("/feign")
@Slf4j
public class DeviceExtraFeignController implements DeviceExtraInterface {
    @Autowired
    private SourceService sourceService;
    @Autowired
    private AnywhereService anywhereService;

    @Autowired
    private MQProducers mqProducers;
    @Autowired
    private GridService gridService;

    @Override
    public Result pageAllSource(@RequestBody PageDeviceParam param) {
        return ResultUtil.success(sourceService.pageAllSource(param));
    }

    @Override
    public Result removeTokenPair(@RequestParam("receiverId") String receiverId, @RequestParam("userId") String userId,
                                  @RequestParam("origin") int origin) {
        anywhereService.removeAllTokenPair(receiverId, userId, origin);
        return ResultUtil.success();
    }

    @Override
    public Result<PageInfo<Device>> pageDeviceOfUser(@RequestBody PageDeviceOfUserParam param) {
        return ResultUtil.success(sourceService.pageDeviceOfUser(param));
    }

    @Override
    public void toRabbitMQByDeviceOnline(@RequestBody String message) {
        mqProducers.toRabbitMQByDeviceOnline(message);

    }

    @Override
    public void toRabbitMQByDeviceLive(@RequestBody String message) {
         mqProducers.toRabbitMQByDeviceLive(message);
    }

    @Override
    public Result<String> devicePairAndStartLive(@RequestBody PariOrRemoveAndStartOrStopLiveParam param) throws Exception {
       return sourceService.devicePairAndStartLive(param);
    }

    @Override
    public Result<String> deviceRemovePairAndStopLive(@RequestBody PariOrRemoveAndStartOrStopLiveParam param) throws Exception {
        return sourceService.deviceRemovePairAndStopLive(param);
    }

    @Override
    public Result<Set<String>> getUserReceiver(@RequestParam("userId") String userId) {
        return sourceService.getUserReceiver(userId);
    }
    @Override
    public Result<List<Device>> getDeviceByUserId(@RequestParam("userId") String userId) {
        return sourceService.getDeviceByUserId(userId);
    }

    @Override
    public Result deleteGird(String rid,String targetId,String pairId) {
        if(StringUtils.isEmpty(rid)&&StringUtils.isEmpty(targetId)&&StringUtils.isEmpty(pairId)){
            return ResultUtil.paramError("至少一个参数");
        }
        return gridService.removePair(rid,targetId,pairId);
    }

    @Override
    public Result<String> startLiveByObjectId(@RequestBody ObjectIdStartLiveParam param) throws Exception {
        return sourceService.startLiveByObjectId(param);
    }

    @Override
    public Result<String> stopLiveByObjectId(@RequestBody ObjectIdStopLiveParam param) throws Exception {
        return sourceService.stopLiveByObjectId(param);
    }
}
